/*
 * 
 */
package de.hauschild.gmltracer.gml;

import java.util.HashMap;
import java.util.Map;

/**
 * The environment for the GML evaluation storing bindings.
 * 
 * @author Klaus Hauschild
 * @since 1.0.0
 */
public class Environment {

  private final Map<String, Token> environment;

  /**
   * Creates a empty environment.
   */
  public Environment() {
    this(new HashMap<String, Token>());
  }

  /**
   * Creates a new environment using all bindings from the given one.
   * 
   * @param theEnvironment
   *          the environment
   */
  public Environment(final Environment theEnvironment) {
    this(theEnvironment.getAll());
  }

  /**
   * Creates a new environment using all 'raw' bindings.
   * 
   * @param theBindings
   *          the bindings
   */
  public Environment(final Map<String, Token> theBindings) {
    environment = new HashMap<String, Token>(theBindings);
  }

  /**
   * Gets the {@link Token} bound to the given identifier. If the identifier can
   * not be found a {@link IllegalArgumentException} will be thrown.
   * 
   * @param identifier
   *          the identifier
   * 
   * @return the token
   */
  public Token get(final String identifier) {
    final Token token = environment.get(identifier);
    if (token == null) {
      throw new IllegalArgumentException(String.format("No token bound to identifier '%s'.", identifier)); //$NON-NLS-1$
    }
    return token;
  }

  /**
   * Gets all 'raw' bindings'.
   * 
   * @return all 'raw' bindings'
   */
  public Map<String, Token> getAll() {
    return environment;
  }

  /**
   * Store in the environment the binding.
   * 
   * @param identifier
   *          the identifier
   * @param token
   *          the token
   */
  public void put(final String identifier, final Token token) {
    environment.put(identifier, token);
  }

  @Override
  public String toString() {
    return environment.toString();
  }
}
